/**
 * 
 */
package com.base.myproject.client.busi.service.analysis;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.JDBCModelData;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.ui.BaseGrid;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.AggregationRowConfig;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.SummaryType;
import com.google.gwt.http.client.URL;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author zgw
 *2009-11-15 上午09:20:09
 * javazgw@gmail.com
 */
public class CcodeActiveAnalysis extends ContentPanel {
	BaseStore<JDBCModelData> store;
	BaseStore<JDBCModelData> storechild;
	
	 TextField<String> name = new TextField<String>();
	 TextField<String> s_cardno= new TextField<String>();
	public CcodeActiveAnalysis() {
		init();
	}
	void init() {
		Date d = new Date();
		
		setWidth("760px");
		setHeading("会员信息查询");
		initstore();
		initstorechild();
		initWidget();
	}
	void initstore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");

		// type.addField("b2cgcode");
		type.addField("ccode");

		type.addField("cname");
		type.addField("email");
		type.addField("s_cardno");
		type.addField("s_sex");
		type.addField("s_edu");
		type.addField("s_birid");
		type.addField("s_mobil");
		type.addField("s_idno");
		type.addField("s_mark");
		type.addField("finannce");
		type.addField("updatedate");
		type.addField("paid");
		type.addField("lastfinance");

		StoreInfo info = new StoreInfo();

		ContentPanel panel = new ContentPanel();

		info.setTablename("sleeping");
		info.setNeedAllRow("no");
		info.setForcenolimit("true");
		 info.setSql(initreloadparam());
		
		store = new BaseStore<JDBCModelData>(type, info,true);

	}
	
	void initstorechild()
	{
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");

		// type.addField("b2cgcode");
		type.addField("finance");

		type.addField("ccode");
		type.addField("updatedate");
		type.addField("dc");
		type.getField("finance").setType(Double.class);

		StoreInfo info = new StoreInfo();


		info.setTablename("aa");
		info.setNeedAllRow("no");
		info.setForcenolimit("true");
		info.setLinkparent_chile_columns(new String[]{"ccode"});
		info.setLinkparentcolumns(new String[]{"ccode"});
		info.setParent(store);
		String sql = " select  finance*dc as finance,ccode,updatedate,case when dc=1 then "+URL.encode("'充'")+" else "+URL.encode("'扣'")+" end as dc from s_service_finance where ccode ='{ccode}'  ";
		sql +=" union all ";
		sql +=" select  paid*-1 as finance,ccode,serviceenddate as updatedate,"+URL.encode("'扣'")+" as dc from s_service_po where  busistatus=524288  and ccode ='{ccode}' order by updatedate desc ";

		 info.setSql(sql);
		
		 storechild = new BaseStore<JDBCModelData>(type, info);
	}
	void initWidget() {
		
		VerticalPanel vpanel = new VerticalPanel();
		
		vpanel.add(new Html("<br>"));
		vpanel.add(initQuery());
		vpanel.add(new Html("<br>"));
		vpanel.add(CreateGrid());
		vpanel.add(CreateGridChile());
		add(vpanel);
		
	}
	Widget CreateGridChile()
	{
		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());
		columns.add(new ColumnConfig("dc", "类型", 50));
		columns.add(new ColumnConfig("finance", "消费/充值金额", 100));
		//columns.add(new ColumnConfig("email", "会员邮箱", 50));
		columns.add(new ColumnConfig("updatedate", "发生日期", 100));
		
		AggregationRowConfig averages = new AggregationRowConfig();
		averages.setHtml("dc", "合计：");
		
		averages.setSummaryType("finance", SummaryType.SUM);
		averages.setSummaryFormat("finance", NumberFormat.getCurrencyFormat());
		
		
		 
		ColumnModel cm = new ColumnModel(columns);
		 cm.addAggregationRow(averages);
		cbs.setSelectionMode(SelectionMode.SINGLE);
		BaseGrid  grid = new BaseGrid(storechild, cm,false,false,null);
		grid.setSelectionModel(cbs);
//		grid.setAutoHeight(true);
//		grid.setAutoWidth(true);
		grid.setSize(760, 200);
		return grid;
	}
	Widget CreateGrid()
	{
		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());
	
		columns.add(new ColumnConfig("cname", "会员名", 50));
		//columns.add(new ColumnConfig("email", "会员邮箱", 50));
		columns.add(new ColumnConfig("s_cardno", "会员卡号", 100));
		columns.add(new ColumnConfig("s_mobil", "移动电话", 100));
		columns.add(new ColumnConfig("updatedate", "最后充值日期", 100));
		columns.add(new ColumnConfig("finannce", "总充值金额", 100));
		
		columns.add(new ColumnConfig("paid", "总消费金额", 100));
		columns.add(new ColumnConfig("lastfinance", "资金余额", 100));
		
		
		columns.add(new ColumnConfig("s_sex", "性别", 50));
//		columns.add(new ColumnConfig("s_edu", "教育程度", 100));
//		columns.add(new ColumnConfig("s_birid", "生日", 100));
		
		columns.add(new ColumnConfig("s_idno", "身份证", 50));
		columns.add(new ColumnConfig("s_mark", "备注", 50));
		
		
		ColumnModel cm = new ColumnModel(columns);
		cbs.setSelectionMode(SelectionMode.SINGLE);
		BaseGrid  grid = new BaseGrid(store, cm,false,false,null);
		grid.setSelectionModel(cbs);
//		grid.setAutoHeight(true);
//		grid.setAutoWidth(true);
		grid.setSize(760,300);
		return grid;
	}
	
	Widget initQuery()
	{
		HorizontalPanel hpanel = new HorizontalPanel();
		hpanel.add(new Label("会员名："));
		
		hpanel.add(name);
		
		
		hpanel.add(new Label("会员卡号："));
	
		hpanel.add(s_cardno);
		
		Button query = new Button("查询");
		hpanel.add(query);
		query.addSelectionListener(new SelectionListener<ButtonEvent>(){

			@Override
			public void componentSelected(ButtonEvent ce) {
				StoreInfo info  = store.getStoreinfo();;
				info.setSql(initreloadparam());
				store.setStoreinfo(info);
				store.reload();
			}
			
		});
		return hpanel;
	}
	String initreloadparam()
	{
		String sql = "select *,b.finannce -isnull(c.paid,0) as lastfinance from ";
		sql +="( ";
		sql +="select ccode,cname,email,s_cardno,case when s_sex=2 then "+URL.encode("'男'")+" else "+URL.encode("'女'")+" end as s_sex,s_edu,s_birid,s_mobil,s_idno,s_mark from ccode where ccode is not null and cname like '"+URL.encode("%"+(name.getValue()==null?"":name.getValue())+"%")+"' and s_cardno like '"+URL.encode("%"+(s_cardno.getValue()==null?"":s_cardno.getValue())+"%")+"'";
		sql +=") as a ";
		sql +="left join ";
		sql +="( ";
		sql +="select sum(isnull(finance*dc,0)) as finannce,ccode,max(updatedate) updatedate from s_service_finance group by ccode ";
		sql +=") as b ";

		sql +="on a.ccode = b.ccode ";
		sql +="left join ";
		sql +="( ";
		sql +="select sum(isnull(paid,0)) as paid,ccode from s_service_po where   busistatus=524288 group by ccode "; 
		sql +=") as c ";
		sql +="on a.ccode = c.ccode";
		return sql;
	}
}
